// KenEgozi.com - my blog engine
// This project is licensed under the MIT License
//
//Copyright (c) 2007, Ken Egozi
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Web;
using System.Collections;
using System.Web.Caching;
using System.Web.SessionState;
using System.Collections.Generic;
using Castle.MonoRail.Framework;
using KenEgozi.Com.Domain.Blog;


namespace KenEgozi.Com.Weblog
{
	/// <summary>
	/// Supply with access to application and session wide parameters,
	/// letting strong typing to the parameters, and hiding the implementetion details 
	/// from the code, (is it in the Session? Cookies? Cache?) 
	/// </summary>
    public static class Repository
    {
		private static KeyComparer keyComparer = new KeyComparer();

		private static IRailsEngineContext Context { get { return MonoRailHttpHandler.CurrentContext; } }
		private static HttpContext HttpContext { get { return HttpContext.Current; } }
		private static HttpSessionState Session { get { return HttpContext.Session; } }
		private static Cache Cache { get { return HttpContext.Cache; } }

		/// <summary>
		/// Current blog. In a multi-blog environment it should consider the blog name
		/// passed in the url on each request
		/// </summary>
		public static Blog Blog
		{
			get
			{
				Blog blog = Cache["Blog"] as Blog;
				if (blog == null)
				{
					blog = Blog.Find(1);
					Cache.Insert("Blog", blog, null, DateTime.Now.AddYears(1), Cache.NoSlidingExpiration);
				}
				return blog;
			}
		}

		/// <summary>
		/// BlogRoll for the current blog. In a multi-blog environment it should consider the blog name
		/// passed in the url on each request
		/// </summary>
		public static BlogRollEntry[] BlogRoll
		{
			get
			{
				BlogRollEntry[] blogRoll = Cache["BlogRoll"] as BlogRollEntry[];
				if (blogRoll == null)
				{
					blogRoll = Blog.FindBlogRoll();
					Cache.Insert("BlogRoll", blogRoll, null, DateTime.Now.AddYears(1), Cache.NoSlidingExpiration);
				}
				return blogRoll;
			}
		}

		/// <summary>
		/// All the blog tags, stored by the url-friendly name of the tag
		/// </summary>
		public static Dictionary<string, Tag> BlogTags
		{
			get
			{
				Dictionary<string, Tag> blogTags = Cache["BlogTags"] as Dictionary<string, Tag>;
				if (blogTags == null)
				{
					Tag[] tags = Tag.FindAll();

					blogTags = new Dictionary<string, Tag>(tags.Length, keyComparer);
					foreach (Tag tag in tags)
						blogTags.Add(tag.UrlFriendlyName, tag);
				}
				Cache.Insert("BlogTags", blogTags, null, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);
				return blogTags;
			}
		}

		/// <summary>
		/// All the blog tags, stored by the tag's name, for dasBlog rerouting purposes
		/// </summary>
		public static Dictionary<string, Tag> BlogTagsByCategoryName
		{
			get
			{
				Dictionary<string, Tag> blogTagsByCategoryName = Cache["BlogTagsByCategoryName"] as Dictionary<string, Tag>;
				if (blogTagsByCategoryName == null)
				{
					blogTagsByCategoryName = new Dictionary<string, Tag>(BlogTags.Count, keyComparer);
					foreach (Tag tag in BlogTags.Values)
						blogTagsByCategoryName.Add(tag.Name, tag);
				}
				Cache.Insert("BlogTagsByCategoryName", blogTagsByCategoryName, null, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);
				return blogTagsByCategoryName;
			}
		}

    }
	class KeyComparer : IEqualityComparer<string>
	{
		#region IEqualityComparer<string> Members
		public bool Equals(string x, string y)
		{
			return x.Equals(y, StringComparison.InvariantCultureIgnoreCase);
		}

		public int GetHashCode(string obj)
		{
			return obj.ToLower().GetHashCode();
		}
		#endregion
	}
}
